//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet.Data
{
    public class QueryTable: IOfficeXmlSerializable
    {
        public QueryTable()
        {

        }
        public QueryTable(string name, DataConnection connection )
        {
            this.Name = name;
            this.Connection = connection;
        }
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("queryTable"))
            {
                autoFormat.ReadXml(c);
                Name = c.GetString("name");
                Headers = c.GetOptionalBoolean("headers", true);
                RowNumbers = c.GetOptionalBoolean("rowNumbers", false);
                RefreshDisabled = c.GetOptionalBoolean("disableRefresh", false);
                BackgroundRefresh = c.GetOptionalBoolean("backgroundRefresh", true);
                FirstBackgroundRefresh = c.GetOptionalBoolean("firstBackgroundRefresh", false);
                RefreshOnLoad = c.GetOptionalBoolean("refreshOnLoad", false);
                this.GrowShrinkType = c.GetOptionalEnum<GrowShrinkType>("growShrinkType", GrowShrinkType.InsertDelete);
                FillFormulas = c.GetOptionalBoolean("fillFormulas", false);
                RemoveDataOnSave = c.GetOptionalBoolean("removeDataOnSave", false);
                DisableEdit = c.GetOptionalBoolean("disableEdit", false);
                PreserveFormatting = c.GetOptionalBoolean("preserveFormatting", true);
                AdjectColumnWidth = c.GetOptionalBoolean("adjustColumnWidth", true);
                Intermediate = c.GetOptionalBoolean("intermediate", false);
                int i = c.GetInteger("connectionId");
                Connection = c.GetData<Workbook>().GetConnectionById(i);
                Refresh = c.ReadOptionalElement<QueryTableRefresh>("queryTableRefresh");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("queryTable"))
            {
                autoFormat.WriteXml(c);
                c.SetString("name", Name);
                c.SetOptionalBoolean("headers", Headers, true);
                c.SetOptionalBoolean("rowNumbers", RowNumbers, false);
                c.SetOptionalBoolean("disableRefresh", RefreshDisabled, false);
                c.SetOptionalBoolean("backgroundRefresh", BackgroundRefresh, true);
                c.SetOptionalBoolean("firstBackgroundRefresh", FirstBackgroundRefresh, false);
                c.SetOptionalBoolean("refreshOnLoad", RefreshOnLoad, false);
                c.SetOptionalEnum<GrowShrinkType>("growShrinkType", this.GrowShrinkType, GrowShrinkType.InsertDelete);
                c.SetOptionalBoolean("fillFormulas", FillFormulas, false);
                c.SetOptionalBoolean("removeDataOnSave", RemoveDataOnSave, false);
                c.SetOptionalBoolean("disableEdit", DisableEdit, false);
                c.SetOptionalBoolean("preserveFormatting", PreserveFormatting, true);
                c.SetOptionalBoolean("adjustColumnWidth", AdjectColumnWidth, true);
                c.SetOptionalBoolean("intermediate", Intermediate, false);
                c.SetInteger("connectionId", Connection.Id);
                c.WriteOptionalElement(Refresh);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        private ExtensionElement _extension;

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }
	


        private DataConnection _connection;

        public DataConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }
	

        private QueryTableRefresh _queryTableRefresh ;

        public QueryTableRefresh Refresh
        {
            get { return _queryTableRefresh; }
            set { _queryTableRefresh = value; }
        }
        private string _name = Guid.NewGuid().ToString().Replace("-", "");

        public string Name
        {
            get { return _name ; }
            set { _name = value; }
        }
        private bool _headers = true;

        public bool Headers
        {
            get { return _headers; }
            set { _headers = value; }
        }
        private bool _rowNumbers;

        public bool RowNumbers
        {
            get { return _rowNumbers; }
            set { _rowNumbers = value; }
        }
        private bool _disableRefresh;

        public bool RefreshDisabled
        {
            get { return _disableRefresh; }
            set { _disableRefresh = value; }
        }
        private bool _backgroundRefresh = true;

        public bool BackgroundRefresh
        {
            get { return _backgroundRefresh ; }
            set { _backgroundRefresh =  value; }
        }
        private bool _firstBackgroundRefresh;

        public bool FirstBackgroundRefresh
        {
            get { return _firstBackgroundRefresh; }
            set { _firstBackgroundRefresh = value; }
        }
        private bool refreshOnLoad;

        public bool RefreshOnLoad
        {
            get { return refreshOnLoad; }
            set { refreshOnLoad = value; }
        }
        private GrowShrinkType _growShrinkType;

        public GrowShrinkType GrowShrinkType
        {
            get { return _growShrinkType; }
            set { _growShrinkType = value; }
        }
        private bool _fillFormulas;

        public bool FillFormulas
        {
            get { return _fillFormulas; }
            set { _fillFormulas = value; }
        }
        private bool removeDataOnSave;

        public bool RemoveDataOnSave
        {
            get { return removeDataOnSave; }
            set { removeDataOnSave = value; }
        }
        private bool _disableEdit;

        public bool DisableEdit
        {
            get { return _disableEdit; }
            set { _disableEdit = value; }
        }
        private bool _preserveFormatting = true;

        public bool PreserveFormatting
        {
            get { return _preserveFormatting; }
            set { _preserveFormatting = value; }
        }
        private bool adjustColumnWidth = true;

        public bool AdjectColumnWidth
        {
            get { return adjustColumnWidth; }
            set { adjustColumnWidth = value; }
        }
        private bool _intermediate;

        public bool Intermediate
        {
            get { return _intermediate; }
            set { _intermediate = value; }
        }

        AGAutoFormat autoFormat = new AGAutoFormat();
        //\\missing AG_AutoFormat
	
	
	
	
	
	
	
    }
    class AGAutoFormat
    {
        private int? _autoFormatId;
        private bool _applyNumberFormats;
        private bool _applyBorderFormats;
        private bool _applyFontFormats;
        private bool _applyPatternFormats;
        private bool _applyAlignmentFormats;
        private bool _applyWidthHeightFormats;

        public void ReadXml (ReadContext c)
        {

            _autoFormatId = c.GetOptionalUIntegerAsInteger("autoFormatId");
            _applyNumberFormats = c.GetOptionalBoolean("applyNumberFormats", false);
            _applyBorderFormats = c.GetOptionalBoolean("applyBorderFormats", false);
            _applyFontFormats = c.GetOptionalBoolean("applyFontFormats", false);
            _applyPatternFormats = c.GetOptionalBoolean("applyPatternFormats", false);
            _applyAlignmentFormats = c.GetOptionalBoolean("applyAlignmentFormats", false);
            _applyWidthHeightFormats = c.GetOptionalBoolean("applyWidthHeightFormats", false);
        }

        public void WriteXml (WriteContext c)
        {

            c.SetOptionalUIntegerAsInteger("autoFormatId", _autoFormatId );
            c.SetBoolean("applyNumberFormats", _applyNumberFormats);
            c.SetBoolean("applyBorderFormats", _applyBorderFormats);
            c.SetBoolean("applyFontFormats", _applyFontFormats);
            c.SetBoolean("applyPatternFormats", _applyPatternFormats);
            c.SetBoolean("applyAlignmentFormats", _applyAlignmentFormats);
            c.SetBoolean("applyWidthHeightFormats", _applyWidthHeightFormats);
            
        }
    }
}
